package com.hyts.bmsp.tool;

import java.lang.reflect.Field;

import com.hyts.bmsp.error.ProcessError;
import com.google.common.base.CaseFormat;
import com.jfinal.plugin.activerecord.Model;

/**
 * @ClassName: JFinalUtil
 * @Description: JFinal工具类
 * @author: 李博/Alex
 * @date: 2017年9月12日 下午9:29:58
 * @email:  alexlibochn@china-pui.com.cn
 */
public final class JFinalUtil {
	
	  /**
	 * @Title: daoUpdateSetHandler
	 * @Description: TODO
	 * 		CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE,“ClassName”)-->class_name
			CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, "class_name")-->className
	 * @param model
	 * @return
	 * @return: Model
	 * @throws IllegalAccessException 
	 * @throws IllegalArgumentException 
	 */
	  public static <T> Model daoUpdateSetHandler(Model model,T t) throws IllegalArgumentException, IllegalAccessException{
		  //校验参数方法
		  if(null == t)
		  {
			  throw new ProcessError("传入的参数为空!");
		  }
		  Field[] fields = t.getClass().getDeclaredFields();
		  for(Field field:fields){
			  field.setAccessible(true);
			  String NoCameraField = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE,field.getName()).toUpperCase();
			  Object fieldValue = field.get(t);
			  if(null!= fieldValue){
				  
				  field.setAccessible(true);
				  model = model.set(NoCameraField, fieldValue);
			  }
		  }
		  return model;
	  }
	
	  /**
	 * @Title: daoUpdateSetHandler
	 * @Description: TODO
	 * @param model
	 * @param t
	 * @param clazz
	 * @return
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @return: V
	 */
	public static <T,V> V daoUpdateSetHandler(Model model,T t,Class<V> clazz) throws IllegalArgumentException, IllegalAccessException{
		  //校验参数方法
		  if(null == t)
		  {
			  throw new ProcessError("传入的参数为空!");
		  }
		  Field[] fields = t.getClass().getDeclaredFields();
		  for(Field field:fields){
			  field.setAccessible(true);
			  String NoCameraField = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE,field.getName()).toUpperCase();
			  Object fieldValue = field.get(t);
			  if(null!= fieldValue){
				  field.setAccessible(true);
				  model = model.set(NoCameraField, fieldValue);
			  }
		  }
		  return (V) model;
	  }
	
	
}
